package com.bike.bikeserver.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bike.bikeserver.common.enums.ModelParamsEnums;
import com.bike.bikeserver.common.utils.ModelUtils;
import com.bike.bikeserver.domain.Zone;
import com.bike.bikeserver.domain.ZoneBikeSummary;
import com.bike.bikeserver.domain.vo.ModelTestImgVo;
import com.bike.bikeserver.domain.vo.ZoneBikeSummaryVo;
import com.bike.bikeserver.mapper.ZoneBikeSummaryMapper;
import com.bike.bikeserver.mapper.ZoneMapper;
import com.bike.bikeserver.service.IZoneBikeSummaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import static com.bike.bikeserver.constant.ModelParamConstant.LSTM_NO_OV_URL;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ZZX
 * @since 2023-08-21
 */
@Service
public class ZoneBikeSummaryServiceImpl extends ServiceImpl<ZoneBikeSummaryMapper, ZoneBikeSummary> implements IZoneBikeSummaryService
{
    @Autowired
    ZoneBikeSummaryMapper zoneBikeSummaryMapper;
    @Autowired
    ZoneMapper zoneMapper;

    @Override
    public List<ZoneBikeSummaryVo> getZoneBikeColumnList()
    {
        List<ZoneBikeSummary> zoneBikeSummaries = zoneBikeSummaryMapper.selectList(null);
        List<ZoneBikeSummaryVo> zoneBikeSummaryVoList = new ArrayList<>();
        for (ZoneBikeSummary zoneBikeSummary : zoneBikeSummaries)
        {
            Zone zone = zoneMapper.selectById(zoneBikeSummary.getZoneId());
            zoneBikeSummaryVoList.add(new ZoneBikeSummaryVo(zone.getZoneName(), ModelParamsEnums.SHARED_BIKE_PATTERN.getDisplayName(), zoneBikeSummary.getSharedBikeNum()));
            zoneBikeSummaryVoList.add(new ZoneBikeSummaryVo(zone.getZoneName(), ModelParamsEnums.PRIVATE_BIKE_PATTERN.getDisplayName(), zoneBikeSummary.getPrivateBikeNum()));
            zoneBikeSummaryVoList.add(new ZoneBikeSummaryVo(zone.getZoneName(), ModelParamsEnums.MOTOR_PATTERN.getDisplayName(), zoneBikeSummary.getMotorNum()));
            zoneBikeSummaryVoList.add(new ZoneBikeSummaryVo(zone.getZoneName(), "总数", zoneBikeSummary.getSumNum()));
        }

        return zoneBikeSummaryVoList;
    }

    @Override
    public List<ZoneBikeSummaryVo> getZoneBikePie()
    {
        List<ZoneBikeSummary> zoneBikeSummaries = zoneBikeSummaryMapper.selectList(null);
        List<ZoneBikeSummaryVo> zoneBikeSummaryVoList = new ArrayList<>();
        Integer sum = 0;
        for (ZoneBikeSummary zoneBikeSummary : zoneBikeSummaries)
        {
            sum += zoneBikeSummary.getSharedBikeNum();
        }
        for (ZoneBikeSummary zoneBikeSummary : zoneBikeSummaries)
        {
            Zone zone = zoneMapper.selectById(zoneBikeSummary.getZoneId());
            zoneBikeSummaryVoList.add(new ZoneBikeSummaryVo(zone.getZoneName(), zone.getZoneName() + "共享单车数量占比", (int) (100.0 * zoneBikeSummary.getSharedBikeNum() / sum)));
        }

        return zoneBikeSummaryVoList;
    }

    @Override
    public List<ZoneBikeSummaryVo> getZoneBikeMultiLine()
    {
        List<ZoneBikeSummaryVo> zoneBikeSummaryVoList = new ArrayList<>();
        List<ZoneBikeSummary> zoneBikeSummaries = zoneBikeSummaryMapper.selectList(null);
        Random random = new Random();
        for (ZoneBikeSummary zoneBikeSummary : zoneBikeSummaries)
        {
            // 2023年8月20月到31月
            for (int i = 20; i <= 31; i++)
            {
                JSONObject params = new JSONObject();
                ModelTestImgVo modelTestImgVo = new ModelTestImgVo(random.nextInt(40), random.nextInt(35), random.nextInt(9), random.nextInt(2), random.nextInt(2), random.nextInt(10), random.nextInt(100), random.nextInt(100), random.nextInt(100));
                List<Integer> periodFlow = new ArrayList<>();
                periodFlow.add(modelTestImgVo.getZoneTemp());
                periodFlow.add(modelTestImgVo.getZoneWeather());
                periodFlow.add(modelTestImgVo.getZonePower());
                periodFlow.add(modelTestImgVo.getIsHoliday());
                periodFlow.add(modelTestImgVo.getIsWeekend());
                periodFlow.add(modelTestImgVo.getNearbyZoneNum());
                periodFlow.add(modelTestImgVo.getK1ZoneFlow());
                periodFlow.add(modelTestImgVo.getK2ZoneFlow());
                periodFlow.add(modelTestImgVo.getK3ZoneFlow());
                List<List<Integer>> finalPeriodFlow = new ArrayList<>();
                finalPeriodFlow.add(periodFlow);
                params.put("period_flow", finalPeriodFlow);
                String lstmResult = ModelUtils.getLstmResult(LSTM_NO_OV_URL, params);
                String[] split = lstmResult.split("：");
                Zone zone = zoneMapper.selectById(zoneBikeSummary.getZoneId());
                zoneBikeSummaryVoList.add(new ZoneBikeSummaryVo("2023-08-" + i, zone.getZoneName(), Integer.parseInt(split[split.length - 1])));
            }
        }
        return zoneBikeSummaryVoList;
    }
}
